package com.guigu.erp.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.erp.mapper.StorageCellMapper;
import com.guigu.erp.pojo.DesignModule;
import com.guigu.erp.pojo.ManufactureDesignProcedure;
import com.guigu.erp.pojo.StorageCell;
import com.guigu.erp.service.StorageCellService;
import com.guigu.erp.utils.PageUtils;
import com.guigu.erp.vo.DesignModuleVo;
import com.guigu.erp.vo.StorageCellVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.NumberUtils;

import java.util.List;

@Service
public class StorageCellServiceImpl extends ServiceImpl<StorageCellMapper, StorageCell>
    implements StorageCellService {
    @Autowired
    private StorageCellMapper storageCellMapper;

    @Override
    public String getMaxDesignId() {
        StorageCell designModule = storageCellMapper.queryMaxModule();
        if (designModule == null){
            return null;
        }
        String designId = designModule.getStoreId();
        return designId;
    }

    @Override
    public PageUtils<StorageCell> getAllSafetyStock(DesignModuleVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<StorageCell> queryWrapper = new QueryWrapper<>();
        //查询条件判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("FIRST_KIND_ID",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("SECOND_KIND_ID",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("THIRD_KIND_ID",vo.getSearch_thirdKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_reviewStatus())){
                queryWrapper.eq("CHECK_TAG",vo.getSearch_reviewStatus());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime1())){
                queryWrapper.ge("REGISTER_TIME",vo.getSearch_registerTime1());
            }
            if(!StringUtils.isEmpty(vo.getSearch_registerTime2())){
                queryWrapper.le("REGISTER_TIME",vo.getSearch_registerTime2());
            }
        }
        //使用分页工具
        PageHelper.startPage(pageNo,pageSize);
        List<StorageCell> list = storageCellMapper.selectList(queryWrapper);
        PageInfo<StorageCell> pageInfo = new PageInfo<>(list);
        PageUtils<StorageCell> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }

    @Override
    public PageUtils<StorageCell> dynamicQueries(StorageCellVo vo, Integer pageNo, Integer pageSize) {
        //封装查询条件
        QueryWrapper<StorageCell> queryWrapper = new QueryWrapper<>();
        //查询条件判断
        if(vo!=null){
            if(!StringUtils.isEmpty(vo.getSearch_firstKindId())){
                queryWrapper.eq("FIRST_KIND_ID",vo.getSearch_firstKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_secondKindId())){
                queryWrapper.eq("SECOND_KIND_ID",vo.getSearch_secondKindId());
            }
            if(!StringUtils.isEmpty(vo.getSearch_thirdKindId())){
                queryWrapper.eq("THIRD_KIND_ID",vo.getSearch_thirdKindId());
            }
            if (!StringUtils.isEmpty(vo.getSearch_name())){
                queryWrapper.like("PRODUCT_NAME","%"+vo.getSearch_name()+"%");
            }
        }
        queryWrapper.eq("CHECK_TAG","S001-1");


        PageHelper.startPage(pageNo,pageSize);
        List<StorageCell> list = storageCellMapper.selectList(queryWrapper);
        PageInfo<StorageCell> pageInfo = new PageInfo<>(list);
        PageUtils<StorageCell> pageUtils = new PageUtils<>();
        pageUtils.setRows(pageInfo.getList());
        pageUtils.setTotal(pageInfo.getTotal());
        return pageUtils;
    }
}
